📚 node [[python loops for loops|for loops]]
Welcome! Nobody has contributed anything to 'python loops for loops|for loops' yet. You can:
  • Write something in the document below!
    • There is at least one public document in every node in the Agora. Whatever you write in it will be integrated and made available for the next visitor to read and edit.
  • Write to the Agora from social media.
    • If you follow Agora bot on a supported platform and include the wikilink [[python loops for loops|for loops]] in a post, the Agora will link it here and optionally integrate your writing.
  • Sign up as a full Agora user.
    • As a full user you will be able to contribute your personal notes and resources directly to this knowledge commons. Some setup required :)
⥅ related node [[python loops for loops]]
⥅ related node [[week3 python for and while loops lab]]
⥅ related node [[20210507122748 for_loops_considered_harmful]]
⥅ node [[python-loops-for-loops]] pulled by Agora

Python Loops - For Loops

Go to the [[Python Week 3 Main Page]] or the [[Python - Main Page]] Also see the [[Programming Main Page]] or the [[Main AI Page]]

Also see [[Python Loops - While Loops]]

For code examples see the [[Week3 Python For and While Loops Lab]]

For Loops

A loop that executes for each element in an iterable.

A graphic illustration of a for loop and syntax

Syntax

for i in range(4)
	nephilim[i] = "Redacted"

Keep in mind, for iterable lists and tuples, you do not need to use the range function if you are iterating through the whole list. An ad-hoc variable representing the elements of the list will do the job instead.

for member in nephilim:
	member = "redacted"

The Range() Function

The range() function outputs an ordered sequence as a list, i, with the same number of elements as the input.

len(range(3)) --> 3 --> [0, 1, 2]

If the range() function has two arguments, where the first argument is smaller than the second argument, than the sequence starts at the first argument and iterates up to but not including the second.

range(15,20) --> [15, 16, 17, 18, 19]

The Enumerate() Function

Provides a counter for a list when looping through it with a for loop.

A graphical representation of enumerate

for i,member in enumerate(nephilim)
	nephilim.name = "Redacted"
	print("Nephilim ", i + 1, " has had their name redacted.")
📖 stoas
⥱ context